#pragma once
#include <functional>
#include <string>
#include <unordered_map>
#include <vector>
class EventDns;
class Service;
struct evdns_request;
struct event;
struct evdns_base;
struct EventDnsCache;
struct evutil_addrinfo;
class EventDnsHandler {
public:
	virtual void onDnsResolve(EventDns* dns, bool isSuccess, const std::string& ip) = 0;
};

class EventDns
{
public:	
	~EventDns();
public:
	static EventDns* query(Service* service, const std::string& hostName, EventDnsHandler* handler);
	static void clear();
	void* data;
private:
	EventDns();
	void decodeResponse(const char * resp, int len);
	void activeCallback();
	static void onRecv(intptr_t fd, short ev, void* args);
	static void onTimer(intptr_t fd, short ev, void* args);
private:
	static std::tr1::unordered_map<std::string, EventDnsCache*> m_mapCache;
	static std::tr1::unordered_map<std::string, EventDns*> m_mapProcessing;
private:
	int m_fd;	 
	event* m_tmr;
	event* m_event;
	EventDnsHandler* m_handler;
	bool m_callbackInTime;
	bool m_isSuccess;
	std::string m_ip;
	std::string m_host;
	std::vector<EventDns*> m_follower;
};

